package com.ycsj.goldmedalnewconcept.fragment;

import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imkit.model.UIConversation;
import io.rong.imkit.widget.adapter.ConversationListAdapter;
import io.rong.imlib.model.Conversation;

import com.ycsj.goldmedalnewconcept.MainActivity;
import com.ycsj.goldmedalnewconcept.StartActivity;
import com.ycsj.goldmedalnewconcept.activity.CreatRedPackageActivity;
import com.ycsj.goldmedalnewconcept.activity.SendRedPackageActivity;
import com.ycsj.goldmedalnewconcept.fragment.ＭessageFragment.ConversationListAdapterEx;
import com.ycsj.goldmedalnewconcept.utils.CircularAnimUtil;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.utils.Utils;
import com.ycsj.goldmedalnewconcept.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

//消息
public class MsgFragment extends Fragment {
	
	private Activity ctx;
	private View layout;
	private MainActivity parentActivity;	
	private ImageView iv_redbag;	
	private String role;	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		if (layout == null) {
			ctx = this.getActivity();
			parentActivity = (MainActivity) getActivity();
			layout = ctx.getLayoutInflater().inflate(R.layout.conversationlist,
					null);
		} else {
			ViewGroup parent = (ViewGroup) layout.getParent();
			if (parent != null) {
				parent.removeView(layout);
			}
		}
		role = SPUtil.getString(getActivity(), "role", "");
		setActionBarTitle(layout) ;
		initView();
		
		return layout;
	}

	/**
     * 设置 actionbar 事件
     * @param view 
     */
    private void setActionBarTitle(View view) {

        RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.rl);
        rl.setVisibility(View.GONE);
//        mBack.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//            	Toast.makeText(getActivity(), TAG, 0).show();
//            }
//        });

    }
	
	private void initView() {
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		
        Bundle args=new Bundle();
		args.putInt("GRADE_ID", 2001);
//		wf.setArguments(args);
		ft.replace(R.id.fl_message, initConversitionList());
		ft.addToBackStack(null);
		ft.commit();
		iv_redbag = (ImageView) layout.findViewById(R.id.iv_redbag);
		
		if("3".equals(role)){
			iv_redbag.setVisibility(iv_redbag.VISIBLE);
		//发红包
		iv_redbag.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
//				Intent intent = new Intent(getActivity(), SendRedPackageActivity.class);
				
				Intent intent = new Intent(getActivity(), CreatRedPackageActivity.class);
//				CircularAnimUtil.startActivityForResult(getActivity(),
//						intent, requestCode, triggerView, colorOrImageRes);
				
				getActivity().startActivity(intent);
			}
		});
		}
	}
	
	private Fragment initConversitionList() {
		 ConversationListFragment fragment = ConversationListFragment.getInstance();
		 fragment.setAdapter(new ConversationListAdapterEx(getActivity()));
	        Uri uri = Uri.parse("rong://" + getActivity().getApplicationInfo().packageName).buildUpon()
	                .appendPath("conversationlist")
	                .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话非聚合显示
	                .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "false")//设置群组会话非聚合显示
	                .appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "false")//设置讨论组会话非聚合显示
	                .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "false")//设置系统会话非聚合显示
	                .build();
	        fragment.setUri(uri);
	        
	        return fragment;
	}
	
	
	  @SuppressLint("NewApi")
		
	    public class ConversationListAdapterEx extends ConversationListAdapter {
	        public ConversationListAdapterEx(Context context) {
	            super(context);
	        }

	        @Override
	        protected View newView(Context context, int position, ViewGroup group) {
	        	
	            return super.newView(context, position, group);
	        }

	        @Override
	        protected void bindView(View v, int position, UIConversation data) {
	        if(data.getConversationType().equals(Conversation.ConversationType.DISCUSSION))
	            data.setUnreadType(UIConversation.UnreadRemindType.REMIND_ONLY);
	            super.bindView(v, position, data);
	        }
	    }
}
